#!/bin/bash
echo "Docker Container Builder powered by Sadam"
((ARGS_LEN = $# - 1))
((REMAINDER = ARGS_LEN % 2))
if [ $REMAINDER == 0 ]; then
  echo "参缺乏"
  exit
else
  echo "参数正确"
fi
OUTER_PORT=3680
CONTAINER_NAME="SADAM-CONTAINER-BUILDER"
IMAGE_NAME="sadam98docker/django"
echo $#,$ARGS_LEN,$REMAINDER,$0,$1,$2

function argsIn() {
  if [ $1 == -p ]; then
    OUTER_PORT=$2
  else
    if [ $1 == -cname ]; then
      CONTAINER_NAME=$2
    else
      if [ $1 == -iname ]; then
        IMAGE_NAME=$2
      fi
    fi
  fi
}
argsIn $1 $2
argsIn $3 $4
argsIn $5 $6

echo $OUTER_PORT,$CONTAINER_NAME,$IMAGE_NAME

argsIn $OUTER_PORT $DOCKER_VOLUMES_DIR
DOCKER_VOLUMES_DIR='/home/docker-volumes'
CONTAINER_DATA_DIR="/usr/src/app"
CURR_CONTAINER_VOLUME_DIR=${DOCKER_VOLUMES_DIR}/${CONTAINER_NAME}
CURR_CONTAINER_VOLUME_DATA_DIR=${CURR_CONTAINER_VOLUME_DIR}/data
CURR_CONTAINER_NET_NAME=${CONTAINER_NAME}-Net

#REDIS_CONF_FILE_NAME='redis.conf'
#REDIS_LOG_FILE_NAME='redis.log'
#REDIS_DOCKER_VOLUME_DIR=${DOCKER_VOLUMES_DIR}/redis
#REDIS_DOCKER_OUTER_DATA_DIR=${REDIS_DOCKER_VOLUME_DIR}/data
#REDIS_DOCKER_OUTER_CONFIG_DIR=${REDIS_DOCKER_VOLUME_DIR}/config
#REDIS_DOCKER_OUTER_CONF_FILE=${REDIS_DOCKER_OUTER_CONFIG_DIR}/${REDIS_CONF_FILE_NAME}
#REDIS_DOCKER_OUTER_LOG_FILE=${REDIS_DOCKER_OUTER_CONFIG_DIR}/${REDIS_LOG_FILE_NAME}
#
#REDIS_DOCKER_INNER_CONFIG_DIR='/usr/local/etc/redis'
#REDIS_DOCKER_INNER_CONF_FILE=${REDIS_DOCKER_INNER_CONFIG_DIR}/${REDIS_CONF_FILE_NAME}
#REDIS_DOCKER_INNER_LOG_FILE=${REDIS_DOCKER_INNER_CONFIG_DIR}/${REDIS_LOG_FILE_NAME}
##REDIS_DOCKER_PID_FILE=${REDIS_DOCKER_VOLUME_DIR}/redis.conf
#

mkdir -p $CURR_CONTAINER_VOLUME_DATA_DIR
#cp "redis.conf" $REDIS_DOCKER_OUTER_CONF_FILE
##echo "" > $REDIS_DOCKER_OUTER_LOG_FILE
#chmod -R 777 $DOCKER_VOLUMES_DIR

#docker pull $IMAGE_NAME
podman pull $IMAGE_NAME

#docker network create $CURR_CONTAINER_NET_NAME
podman network create $CURR_CONTAINER_NET_NAME

#docker run  --name ${CONTAINER_NAME} --net ${CURR_CONTAINER_NET_NAME}  -p ${OUTER_PORT}:${OUTER_PORT} -v ${CURR_CONTAINER_VOLUME_DATA_DIR}:${CONTAINER_DATA_DIR}  -d ${IMAGE_NAME}
podman run -dt --name ${CONTAINER_NAME} --net ${CURR_CONTAINER_NET_NAME} -p ${OUTER_PORT}:${OUTER_PORT} -v ${CURR_CONTAINER_VOLUME_DATA_DIR}:${CONTAINER_DATA_DIR} ${IMAGE_NAME}

#docker ps
podman ps

#docker logs --details ${REDIS_DOCKER_CONTAINER_NAME}
#ls $REDIS_DOCKER_VOLUME_DIR
#ls $REDIS_DOCKER_OUTER_CONFIG_DIR
#ls $REDIS_DOCKER_OUTER_DATA_DIR
